import { IOption, IFormItem, FormItemType } from '@shared/interfaces/common'
import { numScale, compareTwoCurrencies } from 'app/utils/helpers'
import { UPIChannel } from '@shared/interfaces/acquirer'
import { Validators } from '@angular/forms'
import {
  AcquiringIINMaxLength,
  AcquiringIINReg,
  ForwardingIINMaxLength,
  ForwardingIINReg,
  ChannelMerchantNameMaxLength,
  TransactionFeeReg,
  ChannelSignPublicKeyIDMaxLength,
  ChannelSignPublicKeyIDReg,
  ChannelEncryptPublicKeyIDMaxLength,
  ChannelEncryptPublicKeyIDReg,
} from 'app/utils/contraints'
import { Injectable } from '@angular/core'
import { I18NService } from '@core'

@Injectable()
export class UPIService {
  constructor(private i18n: I18NService) {}

  genUPIFormItems = (
    currencyOptions: IOption[],
    currencyCodeOptions: IOption[],
    handleChanMerIDChange,
  ): IFormItem[] => {
    return [
      {
        label: 'Acquiring IIN',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Acquiring IIN' }),
          existed: this.trans('VALIDATION.CODE_EXISTS', { name: 'Acquiring IIN' }),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: AcquiringIINMaxLength }),
          pattern: this.trans('VALIDATION.PATTERN.NUMBER_ONLY'),
        },
        formControlName: 'chanMerID',
        change: handleChanMerIDChange,
      },
      {
        label: 'Forwarding IIN',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Forwarding IIN' }),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: ForwardingIINMaxLength }),
          pattern: this.trans('VALIDATION.PATTERN.NUMBER_ONLY'),
        },
        formControlName: 'chanMerIDSecondary',
      },
      {
        label: 'Transaction Fee',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Transaction Fee' }),
          pattern: this.trans('VALIDATION.PATTERN.FOUR_DECIMAL_MAX_AND_NO_NEGATIVE'),
        },
        type: FormItemType.INPUTGROUP,
        addAfter: '%',
        errTip: `Transaction Fee ${this.trans('VALIDATION.PATTERN.BETWEEN_ZERO_AND_TEN_PERCENT')}`,
        formControlName: 'fee',
        inputType: 'number',
      },
      {
        label: 'Channel Sign Public Key ID',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Channel Sign Public Key ID' }),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: ChannelSignPublicKeyIDMaxLength }),
          pattern: this.trans('VALIDATION.PATTERN.ALPHANUMERIC_ONLY'),
        },
        formControlName: 'signKeyID',
      },
      {
        label: 'Channel Sign Public Key',
        formControlName: 'signKey',
        type: FormItemType.FILE,
      },
      {
        label: 'Channel Sign Encryption Public Key ID',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Channel Sign Encryption Public Key ID' }),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: ChannelEncryptPublicKeyIDMaxLength }),
          pattern: this.trans('VALIDATION.PATTERN.ALPHANUMERIC_ONLY'),
        },
        formControlName: 'encryptKeyID',
      },
      {
        label: 'Channel Encryption Public Key',
        formControlName: 'encryptKey',
        type: FormItemType.FILE,
      },
      {
        label: 'Transaction Currency',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Transaction Currency' }),
        },
        formControlName: 'currencies',
        type: FormItemType.CURRENCY_SELECT,
        multiple: 'multiple',
        allowClear: true,
        compareFn: compareTwoCurrencies,
        options: currencyOptions,
      },
      {
        label: 'Settlement Currency',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Settlement Currency' }),
        },
        formControlName: 'settleCurrency',
        type: FormItemType.SELECT,
        multiple: 'default',
        compareFn: (o1, o2) => o1 === o2,
        options: currencyCodeOptions,
      },
      /*
      {
        label: 'ownerInsCode',
        errTips: {
          required: '必填项',
        },
        errTip: 'chanInsCode 输入不正确',
        formControlName: 'chanInsCode',
      },
      */
      {
        label: 'Channel Merchant Name',
        errTips: {
          required: this.trans('VALIDATION.REQUIRED', { name: 'Channel Merchant Name' }),
          maxLength: this.trans('VALIDATION.MAX_LENGTH', { length: ChannelMerchantNameMaxLength }),
        },
        formControlName: 'chanMerName',
      },
    ]
  }

  genUPIValidateRules = (channel: UPIChannel): { [key: string]: any } => {
    return {
      id: [null, []],
      chanCode: [channel && channel.chanCode, [Validators.required]],
      chanMerID: [
        channel && channel.chanMerID,
        [Validators.required, Validators.maxLength(AcquiringIINMaxLength), Validators.pattern(AcquiringIINReg)],
      ],
      chanMerIDSecondary: [
        channel && channel.chanMerIDSecondary,
        [Validators.required, Validators.maxLength(ForwardingIINMaxLength), Validators.pattern(ForwardingIINReg)],
      ],
      chanMerName: [
        channel && channel.chanMerName,
        [Validators.required, Validators.maxLength(ChannelMerchantNameMaxLength)],
      ],
      fee: [
        channel && numScale(channel.fee, 2),
        [Validators.required, Validators.max(10), Validators.min(0), Validators.pattern(TransactionFeeReg)],
      ],
      currencies: [channel && channel.currencies, [Validators.required]],
      settleCurrency: [channel && channel.settleCurrency, [Validators.required]],
      // chanInsCode: [channel && channel.chanInsCode, [Validators.required]],
      signKey: [channel && channel.signKey, [Validators.required]],
      encryptKey: [channel && channel.encryptKey, [Validators.required]],
      signKeyID: [
        channel && channel.signKeyID,
        [
          Validators.required,
          Validators.maxLength(ChannelSignPublicKeyIDMaxLength),
          Validators.pattern(ChannelSignPublicKeyIDReg),
        ],
      ],
      encryptKeyID: [
        channel && channel.encryptKeyID,
        [
          Validators.required,
          Validators.maxLength(ChannelEncryptPublicKeyIDMaxLength),
          Validators.pattern(ChannelEncryptPublicKeyIDReg),
        ],
      ],
      isLoaded: [ channel && channel.isLoaded],
    }
  }

  private trans(str: string, params?: Object): string {
    return this.i18n.fanyi(str, params)
  }
}
